package com.esint.spring_cloud_leaning.order.controller;

import com.esint.spring_cloud_leaning.order.entity.Product;
import com.esint.spring_cloud_leaning.order.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;

/**
 * @description: 订单controller类
 * @author: 谷明
 * @createDate: 2021/8/7 22:20
 */
@RestController
@RequestMapping("/order")
@RefreshScope // 动态刷新远程配置
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Value("${custom.name}")
    private String aConfigValue;

    // 不用ribbon的调用方式
    @RequestMapping(value = "/way1/buy/{id}", method = RequestMethod.GET)
    public Product findById(@PathVariable Long id){

        //#region DiscoveryClient写法
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        /*for (ServiceInstance instance : instances) {
            System.out.println(instances);
        }*/
        //获取服务实例
        ServiceInstance serviceInstance = instances.get(0);

        //String reqUrl = MessageFormat.format("http://{0}:{1}/product/{2}", serviceInstance.getHost(), Integer.valueOf(serviceInstance.getPort()).toString(), id);
        String reqUrl = MessageFormat.format("{0}/product/{1}", serviceInstance.getUri(), id);
        Product product = restTemplate.getForObject(reqUrl, Product.class);
        return product;
        //#endregion


        //#region restTemplate写法
        /*Product product = restTemplate.getForObject("http://localhost:19001/product/3", Product.class);
        return product;*/
        //#endregion
    }

    // 使用ribbon的调用方式
    @RequestMapping(value = "/way2/buy/{id}", method = RequestMethod.GET)
    public Product findByIdWay2(@PathVariable Long id){
        String reqUrl = MessageFormat.format("http://service-product/product/{0}", id);
        Product product = restTemplate.getForObject(reqUrl, Product.class);
        return product;
    }

    // 使用feign的调用方式
    @RequestMapping(value = "/way3/buy/{id}", method = RequestMethod.GET)
    public Product findByIdWay3(@PathVariable Long id){
        Product product = productFeignClient.findById(id);
        return product;
    }

    @RequestMapping(value = "testConfigDynimic", method = RequestMethod.GET)
    public String getNewestRemoteConfig(){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

//        设置请求参数
        HashMap<String, Object> map = new HashMap<>();
        HttpEntity<HashMap<String, Object>> request = new HttpEntity<>(map, headers);
        restTemplate.postForObject("http://localhost:19002/actuator/refresh", request, Object.class);
        return getAValueFromConfig();
    }

    // 测试读取配置文件值
    @RequestMapping(value = "testConfig", method = RequestMethod.GET)
    public String getAValueFromConfig(){
        return aConfigValue;
    }
}
